package com.sinoglobal.dumping.api;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.dumping.base.Dumpling_ConnectionUtil;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_AdvertisingBean;
import com.sinoglobal.dumping.bean.Dumpling_BaseVo;
import com.sinoglobal.dumping.bean.Dumpling_CardVo;
import com.sinoglobal.dumping.bean.Dumpling_DumpingMakeOrderVo;
import com.sinoglobal.dumping.bean.Dumpling_DumplingNumberBean;
import com.sinoglobal.dumping.bean.Dumpling_DumplingPaySuccessVo;
import com.sinoglobal.dumping.bean.Dumpling_DumplingPayVo;
import com.sinoglobal.dumping.bean.Dumpling_DumplingUserListBean;
import com.sinoglobal.dumping.bean.Dumpling_LogIn_BaseVo;
import com.sinoglobal.dumping.bean.Dumpling_LogOutAcquireDumplingBean;
import com.sinoglobal.dumping.bean.Dumpling_LogOutGetMoneyBean;
import com.sinoglobal.dumping.bean.Dumpling_LogOutMaxDumplingCountBean;
import com.sinoglobal.dumping.bean.Dumpling_NewCardVo;
import com.sinoglobal.dumping.bean.Dumpling_PassportVo;
import com.sinoglobal.dumping.bean.Dumpling_ReceivedCardVo;
import com.sinoglobal.dumping.bean.Dumpling_ShareInfo;
import com.sinoglobal.dumping.bean.Dumpling_ShowCardInfoVo;
import com.sinoglobal.dumping.bean.Dumpling_TimeAction;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_CashDumplingDetailBean;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyCashDumplingBean;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyDumplingBean;
import com.sinoglobal.dumping.net.LogIn_ConnectionUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by KeZhuang on 2015/10/29.
 */
public class Dumpling_RemoteImpl implements Dumpling_IRemote {

    private static Dumpling_RemoteImpl instance = new Dumpling_RemoteImpl();

    public static Dumpling_RemoteImpl getInstance() {
        return instance;
    }

    @Override
    public Dumpling_MyDumplingBean queryMydumpling(String userId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        String json = Dumpling_ConnectionUtil.post(params, "myDumpling");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_MyDumplingBean.class);
    }

    @Override
    public Dumpling_DumplingNumberBean queryDumplingNum() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        String json = Dumpling_ConnectionUtil.post(params, "nowDumplingNumber");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_DumplingNumberBean.class);
    }

    @Override
    public Dumpling_DumplingUserListBean queryDumplingUserList() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        String json = Dumpling_ConnectionUtil.post(params, "acquireDumplingUserList");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_DumplingUserListBean.class);
    }

    @Override
    public Dumpling_LogOutMaxDumplingCountBean queryLogoutCount() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        String json = Dumpling_ConnectionUtil.post(params, "loggedOutUserDumplingToplimit");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_LogOutMaxDumplingCountBean.class);
    }

    @Override
    public Dumpling_LogOutAcquireDumplingBean logoutStart() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        String json = Dumpling_ConnectionUtil.post(params, "loggedOutUserAcquireDumpling");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_LogOutAcquireDumplingBean.class);
    }

    @Override
    public Dumpling_BaseVo addAcquireDumplingNum(String userId, Context context, int num) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("userId", userId);
        params.put("number", num);
        String json = Dumpling_ConnectionUtil.post(params, "addAcquireDumplingChance");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_BaseVo.class);
    }

    @Override
    public Dumpling_LogOutGetMoneyBean logoutGetMoney(String prizeidList) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("prizeidList", prizeidList);
        params.put("phone", Dumpling_SinoConstans.USER_NAME);
        String json = Dumpling_ConnectionUtil.post(params, "loggedOutUserGoAndGet");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_LogOutGetMoneyBean.class);
    }

    @Override
    public Dumpling_LogOutAcquireDumplingBean loginUserDumpling() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("phone", Dumpling_SinoConstans.USER_NAME);
        String json = Dumpling_ConnectionUtil.post(params, "loginUserAcquireDumpling");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_LogOutAcquireDumplingBean.class);
    }

    @Override
    public Dumpling_BaseVo markShareNum(String userId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("userId", userId);
        String json = Dumpling_ConnectionUtil.post(params, "markShare");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_BaseVo.class);
    }

    @Override
    public Dumpling_ShareInfo getShareInfo(String temp_id, String picurl, String starName, String prizeMoney, String allMoney) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("temp_id", temp_id);
        params.put("productid", "");
        params.put("share_plat", "all");
        params.put("picurl", picurl);
        if (!TextUtils.isEmpty(starName) && !TextUtils.isEmpty(prizeMoney)) {
            String requestJson = "{'@starName':'" + starName + "','@prizeMoney':'" + prizeMoney + "'}";
            params.put("parameter_des", requestJson);
        }
        if (!TextUtils.isEmpty(allMoney)) {
            String requestJson = "{'@allMoney':'" + allMoney + "'}";
            params.put("parameter_des", requestJson);
        }
        String json = Dumpling_ConnectionUtil.postAllUrl(params, Dumpling_SinoConstans.SHARE_INTERFACE_HOST, "getShareInfo");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_ShareInfo.class);
    }

    /**
     * 快速登录-账号说明
     */
    @Override
    public Dumpling_PassportVo sinoPassportData(String type) throws Exception {
        String index = "getAgreement";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        String json = LogIn_ConnectionUtil.post(index, params);
        return JSON.parseObject(json, Dumpling_PassportVo.class);
    }

    /**
     * 快速登录-获取验证码
     */
    @Override
    public Dumpling_LogIn_BaseVo NoPasswordLoginCode(String username) throws Exception {
        String index = "findCode";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", username);
        params.put("type", "3");
        String json = LogIn_ConnectionUtil.post(index, params);
        return JSON.parseObject(json, Dumpling_LogIn_BaseVo.class);
    }

    /**
     * 快速登录-登录
     */
    @Override
    public Dumpling_LogIn_BaseVo NoPasswordLogin(String username, String vcode, String proIden) throws Exception {
        String index = "validateLoginCode";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", username);
        params.put("vcode", vcode);
        params.put("proIden", proIden);
        params.put("type", "3");        //找回验证码type
        String json = LogIn_ConnectionUtil.post(index, params);
        return JSON.parseObject(json, Dumpling_LogIn_BaseVo.class);
    }


    /**
     * 2001 用户包饺子生成小锅钱包订单（去支付时调用）
     */
    @Override
    public Dumpling_DumpingMakeOrderVo DumplingMakeOrder(String dumplingMoney, String dumplingNum, String content) throws Exception {
        String index = "redPackets";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("dumplingMoney", dumplingMoney);
        params.put("dumplingNum", dumplingNum);
        params.put("content", content);
        params.put("phone", Dumpling_SinoConstans.USER_NAME);
        String json = Dumpling_ConnectionUtil.post(params, index);
        return JSON.parseObject(json, Dumpling_DumpingMakeOrderVo.class);
    }

    /**
     * 2002 选择支付方式后支付
     */
    @Override
    public Dumpling_DumplingPayVo DumplingMakePayEncrption(String pannikinId, String password, String goodName, String goodDepice, String payChannel, String id, String key) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("pannikinId", pannikinId);
        params.put("password", password);//只有余额支付时，必填
        params.put("tradeId", id);//只有余额支付时，必填
        params.put("goodName", goodName);//只有微信支付时，必填
        params.put("goodDepice", goodDepice);//只有微信支付时，必填
        params.put("payChannel", payChannel);
        String json = Dumpling_ConnectionUtil.postEncrptionDumpling(params, "payment?json=", id, key);
        return JSON.parseObject(json, Dumpling_DumplingPayVo.class);
    }

    /**
     * 2003 用户包饺子后查询支付状态-第三方支付（获取分享链接）
     */
    @Override
    public Dumpling_DumplingPaySuccessVo DumplingPaySuccess(String pannikinId) throws Exception {
        String index = "getPaymentStatus";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("pannikinId", pannikinId);
        String json = Dumpling_ConnectionUtil.post(params, index);
        return JSON.parseObject(json, Dumpling_DumplingPaySuccessVo.class);
    }

    @Override
    public Dumpling_BaseVo changeStatus(String pannikinId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pannikinId", pannikinId);
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        String json = Dumpling_ConnectionUtil.post(params, "changeStatus");
        Log.e("changeStatus","json=="+json);
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_BaseVo.class);
    }


    @Override
    public Dumpling_AdvertisingBean getAdverty() throws Exception {
        String json = Dumpling_ConnectionUtil.post(new HashMap<String, Object>(), "getAdvertising");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_AdvertisingBean.class);
    }

    @Override
    public Dumpling_MyCashDumplingBean queryMyCashdumpling(String userId, int page,
                                                           int pageNum) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("page", page);
        params.put("size", pageNum);
        String json = Dumpling_ConnectionUtil.post(params, "putDumpPage");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_MyCashDumplingBean.class);
    }

    @Override
    public Dumpling_CashDumplingDetailBean queryCashdumplingDetail(String userId, String dumplingUserPutmoneytid, int page,
                                                                   int pageNum) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("dumplingUserPutmoneytid", dumplingUserPutmoneytid);
        params.put("page", page);
        params.put("size", pageNum);
        String json = Dumpling_ConnectionUtil.post(params, "getPutDetailPage");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_CashDumplingDetailBean.class);
    }

    @Override
    public Dumpling_TimeAction getTimeAxis() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", Dumpling_SinoConstans.SYSTYPE);
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        String json = Dumpling_ConnectionUtil.post(params, "getTimeAxis");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_TimeAction.class);
    }

    @Override
    public Dumpling_NewCardVo newCard(String photoUrl, String congratulation, String sendType) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("card_pic", photoUrl);
        params.put("card_wish", congratulation);
        params.put("send_type", sendType);
        String json = Dumpling_ConnectionUtil.post(params, "newCardDumpling");
        JSONObject jsonObject = new JSONObject(json);
        Log.e("Dumpling_NewCardVo",json);
        int code = jsonObject.getInt("code");
        if (100 != code) {
            return null;
        } else {
            String result = jsonObject.getString("resultList");
            Dumpling_NewCardVo newCardVo = null;
            try {
                newCardVo = JSON.parseObject(result, Dumpling_NewCardVo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return newCardVo;
        }
    }
    @Override
    public Dumpling_BaseVo saveCard(String photoUrl, String congratulation, String sendType) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("card_pic", photoUrl);
        params.put("card_wish", congratulation);
        params.put("send_type", sendType);
        String json = Dumpling_ConnectionUtil.post(params, "saveCard");
        JSONObject jsonObject = new JSONObject(json);
        Log.e("Dumpling_SaveCard",json);
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_BaseVo.class);
    }

    @Override
    public Dumpling_ShowCardInfoVo showCard(String cardId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("dumpling_user_put_card_id", cardId);
        String json = Dumpling_ConnectionUtil.post(params, "showCard");
        JSONObject jsonObject = new JSONObject(json);
        int code = jsonObject.getInt("code");
        if (100 != code) {
            return null;
        } else {
            String result = jsonObject.getString("resultList");
            try {
                Dumpling_ShowCardInfoVo newCardVo = JSON.parseObject(result, Dumpling_ShowCardInfoVo.class);
                return newCardVo;
            } catch (Exception e) {
                Log.e("showCard", "JSONException==" + e.getMessage());
                return null;
            }
        }
    }

    @Override
    public Dumpling_BaseVo loginCardDumpling(String cardId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("dumpling_user_put_card_id", cardId);
        String json = Dumpling_ConnectionUtil.post(params, "loginCardDumpling");
        return Dumpling_ConnectionUtil.parseJson(json, Dumpling_BaseVo.class);
    }

    @Override
    public ArrayList<String> getBlessing() throws Exception {
        String json=Dumpling_ConnectionUtil.post(null,"getBlessing");
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONArray jsonArray= jsonObject.getJSONArray("resultList");
            ArrayList<String> arrayList=new ArrayList<String>();
            for (int i=0;i<jsonArray.length();i++) {
                arrayList.add(jsonArray.getString(i));
            }
            return arrayList;
        } catch (JSONException e) {
            Log.e("getBlessing","JSONException=="+e.getMessage());
            return null;
        }
    }

    @Override
    public ArrayList<Dumpling_ReceivedCardVo> myCard(int size) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("size", ""+size);
        params.put("page", "1");
        String json = Dumpling_ConnectionUtil.post(params, "myCard");
        try {
            JSONObject jsonObject=new JSONObject(json);
            String jsonArray=jsonObject.getString("resultList");
            ArrayList<Dumpling_ReceivedCardVo> arrayList= (ArrayList<Dumpling_ReceivedCardVo>) com.alibaba.fastjson.JSONArray.parseArray(jsonArray,Dumpling_ReceivedCardVo.class);
            return arrayList;
        } catch (JSONException e) {
            Log.e("getBlessing","JSONException=="+e.getMessage());
            return null;
        }
    }

    @Override
    public ArrayList<Dumpling_CardVo> putCardPage(int page) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Dumpling_SinoConstans.USER_ID);
        params.put("productCode", Dumpling_SinoConstans.PRODUCT_CODE);
        params.put("sysType", "2");
        params.put("sessionValue", Dumpling_SinoConstans.IMEI);
        params.put("size", "20");
        params.put("page", ""+page);
        String json = Dumpling_ConnectionUtil.post(params, "putCardPage");
        try {
            JSONObject jsonObject=new JSONObject(json);
            String jsonArray=jsonObject.getString("resultList");
            ArrayList<Dumpling_CardVo> arrayList= (ArrayList<Dumpling_CardVo>) com.alibaba.fastjson.JSONArray.parseArray(jsonArray,Dumpling_CardVo.class);
            return arrayList;
        } catch (JSONException e) {
            Log.e("getBlessing","JSONException=="+e.getMessage());
            return null;
        }
    }
}